/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.henryschein.mmvvcc.slave;

import com.henryschein.mmvvcc.model.Person;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.servlet.ServletContext;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 *
 * @author Alexx
 */

public class DBInizializer {
    
    private EntityManagerFactory entityManagerFactory;
    private final static List<String> list = new ArrayList<String>();
    private final static List<String> cat = new ArrayList<String>();
    

    public DBInizializer() {
    }

    public DBInizializer(ServletContext servletContext) {
        ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        entityManagerFactory = (EntityManagerFactory) ctx.getBean("entityManagerFactory");
    }
            
    private static void inizialize(){
        list.add("W - Don't cry because it's over, smile because it happened.");
        list.add("W - I'm selfish, impatient and a little insecure. I make mistakes, I am out of control and at times hard to handle. But if you can't handle me at my worst, then you sure as hell don't deserve me at my best.");
        list.add("W - Those who mind don't matter, and those who matter don't mind.");
        list.add("W - Two things are infinite: the universe and human stupidity; and I'm not sure about the universe.");
        list.add("W - Be yourself; everyone else is already taken.");
        list.add("W - You know you're in love when you can't fall asleep because reality is finally better than your dreams.");
        list.add("W - Be the change that you wish to see in the world.");
        list.add("W - A room without books is like a body without a soul.");
        list.add("W - You only live once, but if you do it right, once is enough.");
        list.add("W - So many books, so little time.");
        list.add("W - In three words I can sum up everything I've learned about life: it goes on.");
        list.add("W - Friendship is born at that moment when one person says to another: What! You too? I thought I was the only one.");
        list.add("W - Don't walk behind me; I may not lead. Don't walk in front of me; I may not follow. Just walk beside me and be my friend.");
        list.add("W - No one can make you feel inferior without your consent.");
        list.add("W - If you tell the truth, you don't have to remember anything.");
        list.add("W - Insanity is doing the same thing, over and over again, but expecting different results.");
        list.add("W - A friend is someone who knows all about you and still loves you.");
        list.add("W - I am so clever that sometimes I don't understand a single word of what I am saying.");
        list.add("W - If you want to know what a man's like, take a good look at how he treats his inferiors, not his equals.");
        list.add("W - Always forgive your enemies; nothing annoys them so much.");
        
        cat.add("Fun");
        cat.add("Popular");
        cat.add("Other");
    }
    
    private void save(Person person) {
        EntityManager entityManager = entityManagerFactory.createEntityManager();
        entityManager.getTransaction().begin();
        entityManager.persist(person);
        entityManager.getTransaction().commit();
    }
    
    public void init() {
        DBInizializer.inizialize();
        for (String s : DBInizializer.getList()) {
            Person p = new Person();
            p.setQuote(s);
            p.setVote("0");
            p.setCategory(DBInizializer.getCat().get((int) (3 * Math.random())));
            save(p);
        }
    }

    public static List<String> getCat() {
        return cat;
    }

    public static List<String> getList() {
        return list;
    }


}
